'use strict'

// 切换工作目录
process.chdir(__dirname)

const titbit = require('titbit');
const fs = require('fs');
const app = new titbit({
    debug: true,
    maxBody: 200000000,
    maxFiles:100
});

// mvc可视化
const tbloader = require('titbit-loader');
let tb = new tbloader();
tb.init(app);


//数据库的连接
const psqlorm = require('psqlorm');
const dbconfig = require('./dbconfig');//数据库配置在dbconfig.js文件里
const initORM = require('psqlorm').initORM;
const pqorm = initORM(dbconfig);//initORM初始化
app.addService('pqorm', pqorm);


// 通过try catch 保证images文件夹存在
// 将前端传递过来的验证码图片把存在imags中
try {
    fs.accessSync('./images');
} catch {
    fs.mkdirSync('./images');
}
app.addService('imagePath', `${__dirname}/images`);

// 读取load页面
app.get('/load', async c => {
    await c.helper.pipe('./load.html', c.reply);
})
app.get('/shown', async c => {
    await c.helper.pipe('./upload.js', c.reply);
})
app.get('/loadcss', async c => {
    await c.helper.pipe('./load.css', c.reply);
})
// index页面
app.get('/index', async c => {
    await c.helper.pipe('./index.html', c.reply);
})
app.get('/indexcss', async c => {
    await c.helper.pipe('./index.css', c.reply);
})

app.get('/verify', async c => {
    // 读取verify页面
    await c.helper.pipe('./verify.html', c.reply);
})


// 运行1234端口
app.run(1234);

const cdpc=require('cdpc');
let cm=new cdpc({
    debug:true,
    notExit:false,
    notWatch: true
})

cm.runChilds([
    {
        name: 'refresh-id',
        file: 'childs/refreshId.js',
        options: {
            cwd:__dirname
        }
    }
])

